package com.model.controller;

import com.entity.Book;
import com.entity.User;
import com.model.service.BookService;
import com.model.service.UserService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;

/**
 * @author jhy
 * @version 1.0
 * @since 1.8
 */
@Controller
public class Ctrl {
    @Resource
    BookService bookService;

    @Resource
    UserService userService;

    @RequestMapping("/list")
    public String list(Model model){
        model.addAttribute("books",bookService.listAll());
        return "list";
    }
    @RequestMapping("/enter")   //与/list方法相同，不同点拦截器会阻挡
    public String enter(){
        return "redirect:/list";
    }
    @RequestMapping("/add")
    public String add(Model model){
        return "information";
    }
    @RequestMapping("/set")
    public String set(Integer id, Model model){
        model.addAttribute("book",bookService.listOne(id));
        return "information";
    }
    @RequestMapping("/drop")
    public String drop(Integer id, Model model){
        bookService.dropOne(id);
        return "redirect:/list";
    }
    @RequestMapping("/save")
    public String save(Integer id, String bookName, String author, Double price, String press, String pressDate, Model model){
        if(id==null||"".equals(id.toString())){
            Book book = new Book();
            book.setBookName(bookName);
            book.setAuthor(author);
            book.setPrice(price);
            book.setPress(press);
            if(pressDate==null||"".equals(pressDate)){
                book.setPressDate(null);
            }else{
                Date date = null;
                try {
                    date = Date.valueOf(pressDate);
                    book.setPressDate(date);
                } catch (Exception e) {
                    e.printStackTrace();
                    model.addAttribute("book",book);
                    model.addAttribute("errorMsg","日期格式不正确，正确格式:yyyy-mm-dd");
                    return "information";
                }
            }
            try {
                bookService.addOne(book);
            } catch (Exception e) {
                e.printStackTrace();
                book.setBookName(null);
                model.addAttribute("book",book);
                model.addAttribute("errorMsg","书名重复");
                return "information";
            }
        }
        else{
            Book book = new Book();
            book.setId(id);
            book.setBookName(bookName);
            book.setAuthor(author);
            book.setPrice(price);
            book.setPress(press);
            if(pressDate==null||"".equals(pressDate)){
                book.setPressDate(null);
            }else{
                Date date = null;
                try {
                    date = Date.valueOf(pressDate);
                    book.setPressDate(date);
                } catch (Exception e) {
                    e.printStackTrace();
                    model.addAttribute("book",book);
                    model.addAttribute("errorMsg","日期格式不正确，正确格式:yyyy-mm-dd");
                    return "information";
                }
            }
            bookService.setOne(book);
        }
        return "redirect:/list";
    }
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
    @RequestMapping("/apply")
    public String apply(String name, String username, String password, String repeatPassword, Model model, HttpServletRequest request){
        if(!password.equals(repeatPassword)){
            model.addAttribute("errorMsg","两次密码输入不同");
            return "register";
        }
        User user = new User();
        user.setName(name);
        user.setUsername(username);
        user.setPassword(password);
        try {
            userService.insertUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("errorMsg","用户名已存在");
            return "register";
        }
        HttpSession session = request.getSession();
        session.setAttribute("Login",true);
        session.setAttribute("name",user.getName());
        return "redirect:/list";
    }
    @RequestMapping("/exit")
    public String exit(HttpServletRequest request){
        request.getSession().invalidate();
        return "redirect:/list";
    }
}
